package com.leather.question.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leather.common.entity.Answer;
import com.leather.common.entity.Question;
import com.leather.common.result.CodeMsg;
import com.leather.common.result.Result;
import com.leather.common.toolkits.BeanKit;
import com.leather.common.toolkits.CheckKit;
import com.leather.common.vo.MessageVo;
import com.leather.question.config.MQConfig;
import com.leather.question.mapper.AnswerMapper;
import com.leather.question.mapper.QuestionMapper;
import com.leather.question.service.QuestionService;
import com.leather.question.vo.QuestionAddVo;
import com.leather.question.vo.QuestionQueryVo;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Random;

@Service
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> implements QuestionService {

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private AmqpTemplate amqpTemplate;


    @Autowired
    private AnswerMapper answerMapper;


    @Override
    public Result index(int pageId) {
        return Result.success(questionMapper.index(pageId));
    }

    @Override
    public Result addQuestion(QuestionAddVo questionAddVo) {
        Result result = questionAddVo.validate();
        if (!result.isSuccess()) {
            return result;
        }
        Question question = (Question) result.getData();
        CheckKit.check(save(question), CodeMsg.SERVER_ERROR);
        return Result.success(question.getId());
    }

    @Override
    public Result star(int questionId) {
        int count = questionMapper.star(questionId);
        return Result.success(count);
    }

    @Override
    public Result deleteQuestion(int questionId, int userId) {
        CheckKit.check(questionId > 0, CodeMsg.ERROR_ANSWER_ID);
        CheckKit.check(userId > 0, CodeMsg.ERROR_USER_ID);
        int count = questionMapper.deleteQuestion(questionId, userId);
        CheckKit.check(count > 0, CodeMsg.SERVER_ERROR);
        amqpTemplate.convertAndSend(MQConfig.DELETE_ANSWER_QUENE, questionId);
        return Result.success("ok");
    }

    @Override
    public Result getQuestionById(int questionId) {
        CheckKit.check(questionId > 0, CodeMsg.ERROR_QUESTION_ID);
        QuestionQueryVo questionQueryVo = questionMapper.getByQuestionId(questionId);
        CheckKit.check(questionQueryVo != null, CodeMsg.QUESTION_DELETED);
        return Result.success(questionQueryVo);
    }

    @Override
    public Result getUserQuestion(int userId, Page<Question> pages) {
        CheckKit.check(userId > 0, CodeMsg.ERROR_USER_ID);
        IPage<Question> questions = query().eq("user_id", userId).page(pages);
        return Result.success(questions.getRecords());
    }

    @Override
    public Result getCount(int userId) {
        CheckKit.check(userId > 0, CodeMsg.ERROR_USER_ID);
        int questionCount = query().eq("user_id", userId).count();
        QueryWrapper<Answer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        int answerCount = answerMapper.selectCount(queryWrapper);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("questions", questionCount);
        jsonObject.put("answers", answerCount);
        return Result.success(answerCount);
    }

    @Override
    public Result getInviteUser(int questionId,int userId) {

        int value = new Random().nextInt(8);
        String order = "";
        switch (value) {
            case 0:
                order = "id";
                break;
            case 1:
                order = "nick_name";
                break;
            case 2:
                order = "real_name";
                break;
            case 3:
                order = "phone";
                break;
            case 4:
                order = "description";
                break;
            case 5:
                order = "department";
                break;
            case 6:
                order = "college";
                break;
            case 7:
                order = "create_time";
                break;
        }
        return Result.success(questionMapper.inviteUser(questionId,userId,order));
    }

    @Override
    public Result inviteUsers(int questionId, int userId,int inviteId) {
        MessageVo messageVo = new MessageVo();
        messageVo.setMessageType(0);
        messageVo.setMessageTitle("[问答通知]");
        messageVo.setMessageContent("有人邀请你回答问题,点击立即回答");
        messageVo.setMessageUrl("/pages/question/questiondetail?questionId=" + questionId);
        messageVo.setUserId(inviteId);
        questionMapper.insertInvite(userId,inviteId,questionId);
        amqpTemplate.convertAndSend(MQConfig.MESSAGE_QUENE, BeanKit.beanToString(messageVo));
        return Result.success("ok");
    }
}
